package my.algorithms.mcorrea.topcoder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 *A busy businessman has a number of equally important tasks which he must accomplish. 
 * To decide which of the tasks to perform first, he performs the following operation.

He writes down all his tasks in the form of a circular list, so the first task is adjacent 
* to the last task. He then thinks of a positive number. This number is the random seed, 
* which he calls n. Starting with the first task, he moves clockwise 
* (from element 1 in the list to element 2 in the list and so on), 
* counting from 1 to n. When his count reaches n, he removes that task from the list 
* and starts counting from the next available task. He repeats this procedure until 
* one task remains. It is this last task that he chooses to execute.

Given a String[] list representing the tasks and an int n, return the task which the 
* businessman chooses to execute.
 * @author mcorrea
 */
public class BusinessTasks {

    public static void main(String[] args) {
		System.out.println(1001%2);
		
		String[] list = {"a","b","c","d"};
		System.out.println(getTask(list, 2));
		
		String[] list2 = {"a","b","c","d","e"};
		System.out.println(getTask(list2, 3));
		
		String[] list3 = {"alpha","beta","gamma","delta","epsilon"};
		System.out.println(getTask(list3, 1));
		
		String[] list4 = {"a","b"};
		System.out.println(getTask(list4, 1000));
		
		String[] list5 = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z"};
		System.out.println(getTask(list5, 17));
		
		String[] list6 = {"zlqamum","yjsrpybmq","tjllfea","fxjqzznvg","nvhekxr","am","skmazcey","piklp",
"olcqvhg","dnpo","bhcfc","y","h","fj","bjeoaxglt","oafduixsz","kmtbaxu",
"qgcxjbfx","my","mlhy","bt","bo","q"};
		System.out.println(getTask(list6, 9000000));
		
		
    }

	public static String getTask(String[] list, int n){
		
		List<String> tasks = new ArrayList<String>(); 
		tasks.addAll(Arrays.asList(list));
		int k = 0;
		while(tasks.size() > 1){
			int rindex = (k+n-1)%tasks.size();
			tasks.remove(rindex);
			k = rindex%tasks.size();
		}
		
		return tasks.get(0);
	}
	
	
}
